package com.changanw.dynamic.thread.pool.sdk.registry.redis;

import com.changanw.dynamic.thread.pool.sdk.domain.model.RegistryEnumVO;
import com.changanw.dynamic.thread.pool.sdk.domain.model.ThreadPoolConfigEntity;
import com.changanw.dynamic.thread.pool.sdk.registry.IRegistry;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;

import java.time.Duration;
import java.util.List;

/**
 * @author changanw
 * @description redis注册中心
 * @date 2024/5/16 9:59
 */
public class RedisRegistry implements IRegistry {
  private final RedissonClient redissonClient;

  public RedisRegistry(RedissonClient redissonClient) {
    this.redissonClient = redissonClient;
  }
  @Override
  public void reportThreadPool(List<ThreadPoolConfigEntity> threadPoolEntities) {
    RList<ThreadPoolConfigEntity> list = redissonClient.getList(RegistryEnumVO.THREAD_POOL_CONFIG_LIST_KEY.getKey());
    //去重
    list.delete();
    list.addAll(threadPoolEntities);
  }

  @Override
  public void reportThreadPoolConfigParameter(ThreadPoolConfigEntity threadPoolConfigEntity) {
    String cacheKey = RegistryEnumVO.THREAD_POOL_CONFIG_PARAMETER_LIST_KEY.getKey() + "_" + threadPoolConfigEntity.getAppName() + "_" + threadPoolConfigEntity.getThreadPoolName();
    RBucket<ThreadPoolConfigEntity> bucket = redissonClient.getBucket(cacheKey);
    bucket.set(threadPoolConfigEntity, Duration.ofDays(30));
  }
}
